import 'dart:async';
import 'package:flutter/material.dart';
import 'package:school_assistant_m/utils/pp_table_element.dart';

// 列表聚合上拉加载下拉刷新

class PpTableList extends StatefulWidget {


  // 传入参数
  const PpTableList({
    Key? key,
    required this.dataForm,// 数据列表[{},{}……]
    this.frontHeight = 50.0, // 前置高度，避开搜索栏
    required this.pullup, // 上拉回调函数
    required this.dropdown,// 下拉回调函数
    // required this.onPressed,// 点击回调函数
    required this.onTap,// 点击回调函数
    this.name = 'name',
    this.code = 'code',
    this.itemid = 'id',
    this.templateId = 'templateId',
    this.user = 'user',
    this.site = 'site',
    this.time = 'time',
    this.startTime = 'startTime',
    this.endTime = 'endTime',
    this.result= 'result',
    this.reportContent = 'reportContent',
    this.tag = 'status',
    this.nfc= 'nfc',
  }) : super(key: key,);
  final List dataForm;
  final double frontHeight;
  final ValueChanged<int> onTap;
  final VoidCallback pullup;
  final VoidCallback dropdown;//ValueChanged dropdown;//
  final String name;
  final String code;
  final String itemid;
  final String templateId;
  final String user;
  final String site;
  final String nfc;
  final String time;
  final String startTime;
  final String endTime;
  final String result;
  final String reportContent;
  final String tag;

  @override
  State<StatefulWidget> createState() => PpTableListState();
}

// 构建
class PpTableListState extends State<PpTableList> {
  // 上拉加载回调函数
  final ScrollController _controller = ScrollController();
  // 上拉加载更多
  _loadMore() async {
    /// 强制休眠 1 秒
    await Future.delayed(Duration(seconds: 1));
    /// 放入到集合中
    setState(() {widget.pullup();});
    setState((){});
    // setState(() {_DataRestart();});
  }

  @override
  void initState() {
    /// 为滚动控制器添加监听
    _controller.addListener(() {
      // var maxScroll = _controller.position.maxScrollExtent;
      // var pixel = _controller.position.pixels;

      // 在加载的时候锁死
      if (_controller.position.pixels ==
          _controller.position.maxScrollExtent) {
        /// 触发上拉加载更多机制
        _loadMore();
      }
    });
    setState((){});
    super.initState();
  }
  @override
  void dispose() {
    // 销毁 滚动控制器 ScrollController
    _controller.dispose();
    super.dispose();
  }

  makeList() {
    List<Widget> theList = [];
    theList = [
      SizedBox(height: widget.frontHeight,),// 前置高度
    ];

    List<String> choices = ['done','wait'];
    Map choicesValues = {'done':' 已巡查 ','wait':' 未巡查 '};

    for(var item in widget.dataForm) {
      theList.add(
        PpTableElement(
          templateId:item[widget.templateId].toString(),
          checkName:item[widget.user].toString(),
          checkSite:item[widget.site].toString(),
          checkNfc: item[widget.nfc].toString(),
          checkTime:item[widget.time].toString(),
          checkStartTime:item[widget.startTime].toString(),
          checkEndTime:item[widget.endTime].toString(),
          result:item[widget.result].toString(),
          tag:choices.contains(item[widget.tag].toString()) ?choicesValues[item[widget.tag].toString()]:' ',
          // reportContent:widget.reportContent.isEmpty?[]:item[widget.reportContent],
          reportContent:item[widget.reportContent],
          onTap:(){widget.onTap(item[widget.itemid]);},
        )
      );
    }
    theList.add(SizedBox(height: 50,));
    return theList;
  }

  Future<void> _onRefresh() async {
    await Future.delayed(Duration(seconds: 1));// 强制休眠 1 秒
    setState(() {widget.dropdown();});
    return;
  }

  @override
  Widget build(BuildContext context) {

    return RefreshIndicator(
      color: Colors.pink,//圆圈进度颜色
      displacement: 150.0,//下拉停止的距离
      backgroundColor: Colors.grey[200],//背景颜色
      onRefresh: _onRefresh,
      child: ListView(
        controller: _controller,
        children: makeList(),
      ),
    );
  }
}